<?php
//PingBuWang.Com
//Copyright (c) 2017~2099 http://www.pingbuwang.com All rights reserved.
namespace app\article\tags;

use think\template\TagLib;

class TagArticle extends TagLib {

    protected $tags   =  [
        'get_articles_info'      => ['attr' => 'value,where', 'close' => 0],
        'get_list'      => ['attr' => 'value,where,order,page,limit', 'close' => 0],
        'get_page'      => ['attr' => 'value,where,order,page,limit', 'close' => 0],
        'search'      => ['attr' => 'value,where,order,page,limit', 'close' => 0],
        'get_by_id'      => ['attr' => 'value,article_id', 'close' => 0],
        'get_one'      => ['attr' => 'value,where,order', 'close' => 0],
        'add_views'      => ['attr' => 'article_id', 'close' => 0],
        'get_prev'      => ['attr' => 'value,article_id', 'close' => 0],
        'get_next'      => ['attr' => 'value,article_id', 'close' => 0],
    ];

    public function tagGet_articles_info($tag){
        $value = $tag['value'];
        $where = (!empty($tag['where'])?$tag['where']:"null" ) ;

        $html  = '<?php $'.$value.' = model("app\article\model\Article")->getArticlesInfo( '.$where.'); ?> ';
        return $html;
    }

    public function tagGet_List($tag){
        $value = $tag['value'];
        $where = (!empty($tag['where'])?$tag['where']:"null" ) ;
        $order1 = (!empty($tag['order'])?$tag['order']:"'sort desc'" );
        $page = ( !empty($tag['page'])?$tag['page']:'null' );
        $limit = ( !empty($tag['limit'])?$tag['limit']:100 );
        $q = ( !empty($tag['q'])?$tag['q']:'null' );

        $html  = '<?php $'.$value.' = model("app\article\model\Article")->getList( '.$where.','.$order1.','.$page.','.$limit.','.$q.'); ?> ';
        return $html;

    }

    public function tagGet_Page($tag){
        $value = $tag['value'];
        $where = (!empty($tag['where'])?$tag['where']:"null" ) ;
        $order1 = (!empty($tag['order'])?$tag['order']:"'sort desc'" );
        $page = ( !empty($tag['page'])?$tag['page']:1 );
        $limit = ( !empty($tag['limit'])?$tag['limit']:15 );
        $q = ( !empty($tag['q'])?$tag['q']:'null' );

        $html  = '<?php $'.$value.' = model("app\article\model\Article")->getPage( '.$where.','.$order1.','.$page.','.$limit.','.$q.');  ?> ';
        return $html;

    }

    public function tagSearch($tag){
        $value = $tag['value'];
        $q = ($tag['q']) ;
        $where = "null";  //"\"id in (select id from ".config('database')['prefix']."articles_content where content like  '%\"." .$q.".\"%' )\"" ;

        //
        $order1 = 'id desc' ;
        $page = !empty($tag['page'])?$tag['page']:1 ;
        $limit = ( !empty($tag['limit'])?$tag['limit']:15 );
        $q = ( !empty($tag['q'])?$tag['q']:'null' );

        $html  = '<?php $'.$value.' = model("app\article\model\Article")->getPage( '.$where.','.$order1.','.$page.','.$limit.','.$q.');  ?> ';
        return $html;

    }

    public function tagget_by_id($tag){
        $value = $tag['value'];
        $articleId = ($tag['article_id']);
        $q =  !empty($tag['q'])?$tag['q']:'null';

        $html  = '<?php $'.$value.' = model("app\article\model\Article")->getById('.$articleId.','.$q.'); ?> ';
        return $html;
    }
    public function tagget_one($tag){
        $value = $tag['value'];
        $where =  !empty($tag['where'])?$tag['where']:'null';
        $order1 = (!empty($tag['order'])?$tag['order']:"'sort desc'" );
        $q =  !empty($tag['q'])?$tag['q']:'null';

        $html  = '<?php $'.$value.' = model("app\article\model\Article")->getOne('.$where.','.$order1.','.$q.'); ?> ';
        return $html;
    }

    public function tagadd_views($tag){
        $articleId = ($tag['article_id']);
        $where =  !empty($tag['where'])?$tag['where']:'null';

        $html  = '<?php $c1 = model("app\article\model\Article")->updateViews('.$articleId.','.$where.'); ?> ';
        return $html;
    }

    public function tagget_prev($tag){
        $articleId = $tag['article_id'];
        $value = $tag['value'];

        $html  = '<?php  $'.$value.' = model("app\article\model\Article")->getPrev('.$articleId.' ); ?> ';
        return $html;

    }

    public function tagget_next($tag){
        $articleId = $tag['article_id'];
        $value = $tag['value'];

        $html  = '<?php $'.$value.' = model("app\article\model\Article")->getNext('.$articleId.' ); ?> ';
        return $html;

    }

}
